import 'package:citiesocial/_include.dart';

class CatalogueNext extends BaseStatefulWidget {
  final String pid, title;
  CatalogueNext(this.pid, this.title);
  @override
  State<StatefulWidget> createState() => _CatalogueNextState();
}

class _CatalogueNextState extends BaseState<CatalogueNext> {
  final _list = List<CategoryEntity2>();
  _CatalogueNextState();

  void _loadData() async {
    LoadingDialog.show(context);
    final list =
        await HttpManager.directoryTwoCategory(widget.pid, context: context);
    if (list != null && list.length > 0) {
      _list.clear();
      setState(() {
        _list.addAll(list);
      });
    }
    LoadingDialog.dismiss(context);
  }

  @override
  void initState() {
    Future.delayed(Configs.loadDelay, () {
      _loadData();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarUtils.withCart(context, title: '${widget.title}'),
      backgroundColor: Colors.white,
      body: SafeArea(
        child: ListView.builder(
          itemCount: _list.length,
          itemBuilder: (_, index) => ExpansionTile(
                title: Container(
                  child: Text(
                    '${_list[index].cat_name}',
                    style: TextStyle(fontSize: 18.0),
                  ),
                  margin: EdgeInsets.symmetric(horizontal: 10.0),
                ),
                children: List.generate((_list[index].child_cat?.length ?? 0),
                    (inner) {
                  return ListTile(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (_) =>
                              GoodsByCatalogue(_list[index].child_cat[inner]),
                        ),
                      );
                    },
                    trailing: Image.asset(
                      'imgs/js_cells_img_more_g.png',
                      width: 18.0,
                      height: 18.0,
                      fit: BoxFit.scaleDown,
                    ),
                    title: Container(
                      margin: EdgeInsets.only(left: 20.0, right: 10.0),
                      child: Text(
                        '${_list[index].child_cat[inner].cat_name}',
                        style: TextStyle(fontSize: 17.0),
                      ),
                    ),
                  );
                }),
              ),
        ),
      ),
    );
  }
}
